package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbMemoryStandard;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbMemoryStandardRepository extends JpaRepository<CmdbMemoryStandard, Integer>, JpaSpecificationExecutor<CmdbMemoryStandard> {
    List<CmdbMemoryStandard> findAllByIdIn(List<Long> id);

    @Query(value = "select m from CmdbMemoryStandard m where m.isDelete = 0 " +
            "and (:#{#paramSearchDto.memoryStandardCode} is null or m.memoryStandardCode like concat('%', :#{#paramSearchDto.memoryStandardCode}, '%')) " +
            "and (:#{#paramSearchDto.name} is null or m.name LIKE CONCAT('%',:#{#paramSearchDto.name},'%')) " +
            "and (:#{#paramSearchDto.brand} is null or m.brand = :#{#paramSearchDto.brand}) " +
            "and (:#{#paramSearchDto.partNum} is null OR m.partNum LIKE CONCAT('%',:#{#paramSearchDto.partNum}, '%')) " +
           "and (:#{#paramSearchDto.standardNum} is null or m.standardNum LIKE CONCAT('%', :#{#paramSearchDto.standardNum}, '%')) " +
            "and (:#{#paramSearchDto.hz} is null or m.hz = :#{#paramSearchDto.hz}) " +
            "and (:#{#paramSearchDto.capacity} is null or m.capacity = :#{#paramSearchDto.capacity}) ")
    Page<CmdbMemoryStandard> searchByParam(Pageable pageable, CmdbMemoryStandard paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_memory_standard set is_delete = :deleted where memory_standard_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    CmdbMemoryStandard findOneByMemoryStandardCode(String memoryStandardCode);

    List<CmdbMemoryStandard> findAllByNameAndIsDelete(String name, Integer noDelete);

    List<CmdbMemoryStandard> findAllByPartNumAndIsDelete(String partNum, Integer noDelete);
}

